package com.yuanheng100.ems4j.service;

import com.dingtalk.open.client.api.model.corp.*;
import com.yuanheng100.ems4j.api.IDingTalkService;
import com.yuanheng100.ems4j.common.constant.MessageStatus;
import com.yuanheng100.ems4j.dingtalk.DingTalk;
import com.yuanheng100.ems4j.mapper.Ems4jDingTalkMapper;
import com.yuanheng100.ems4j.model.Ems4jDepartment;
import com.yuanheng100.ems4j.model.Ems4jDingTalk;
import com.yuanheng100.ems4j.model.Ems4jEmployee;
import com.yuanheng100.ems4j.model.Ems4jPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by jlqian on 2017/1/16.
 */
public class DingTalkServiceImpl implements IDingTalkService
{
    private static Logger logger = LoggerFactory.getLogger(DingTalkServiceImpl.class);

    @Autowired
    private Ems4jDingTalkMapper ems4jDingTalkMapper;

    private DingTalk dingTalk = null;

    public void setDingTalk(DingTalk dingTalk)
    {
        this.dingTalk = dingTalk;
    }

    @Override
    public Long createDepartment(String name, String parentId, Integer order, Boolean createDeptGroup)
    {
        return dingTalk.createDepartment(name, parentId, order, createDeptGroup);
    }

    @Override
    public void deleteDepartment(Long departmentId)
    {
        dingTalk.deleteDepartment(departmentId);
    }

    @Override
    public void updateDepartment(String name, Long departmentId, Integer parentId, Integer order, Boolean createDeptGroup, Boolean autoAddUser, String deptManagerUseridList, Boolean deptHiding, String deptPerimits)
    {
        dingTalk.updateDepartment(name, departmentId, parentId, order, createDeptGroup, autoAddUser, deptManagerUseridList, deptHiding, deptPerimits);
    }

    @Override
    public List<Ems4jDepartment> listDepartments(String parentDeptId)
    {
        List<Department> departments = dingTalk.listDepartments(parentDeptId);
        ArrayList<Ems4jDepartment> ems4jDepartments = new ArrayList<Ems4jDepartment>();
        for (Department department : departments)
        {
            ems4jDepartments.add(convertDepartment2Ems4jDepartment(department));
        }
        return ems4jDepartments;
    }

    @Override
    public Ems4jDepartment getDepartmentDetail(String deptId)
    {
        DepartmentDetail departmentDetail = dingTalk.getDepartmentDetail(deptId);
        return convertDepartmentDetail2Ems4jDepartment(departmentDetail);
    }

    private Ems4jDepartment convertDepartment2Ems4jDepartment(Department department)
    {
        Ems4jDepartment ems4jDepartment = new Ems4jDepartment();
        ems4jDepartment.setId(department.getId().intValue());
        ems4jDepartment.setName(department.getName());
        Long parentid = department.getParentid();
        ems4jDepartment.setParentid(parentid == null ? 0 : parentid.intValue());
        return ems4jDepartment;
    }

    private Ems4jDepartment convertDepartmentDetail2Ems4jDepartment(DepartmentDetail department)
    {
        Ems4jDepartment ems4jDepartment = new Ems4jDepartment();
        ems4jDepartment.setId(department.getId().intValue());
        ems4jDepartment.setName(department.getName());
        Long parentid = department.getParentid();
        ems4jDepartment.setParentid(parentid == null ? 0 : parentid.intValue());
        ems4jDepartment.setOrdernum(department.getOrder().shortValue());
        return ems4jDepartment;
    }

    @Override
    public void createUser(String userid, String name, String email, String mobile, List<Long> departments)
    {
        dingTalk.createUser(userid, name, email, mobile, departments);
    }

    @Override
    public void deleteUser(String userid)
    {
        dingTalk.deleteUser(userid);
    }

    @Override
    public void batchDeleteUser(List<String> useridlist)
    {
        dingTalk.batchDeleteUser(useridlist);
    }

    @Override
    public void updateUser(String userid, String name, List<Long> department, String email, String mobile)
    {
        dingTalk.updateUser(userid, name, department, email, mobile);
    }

    @Override
    public Ems4jEmployee getUser(String userid)
    {
        CorpUserDetail user = dingTalk.getUser(userid);
        return convertCorpUser2Ems4jEmployee(user);
    }

    @Override
    public List<Ems4jEmployee> getDepartmentUser(Long departmentId, Long offset, Integer size, Integer order)
    {
        CorpUserList departmentUser = dingTalk.getDepartmentUser(departmentId, offset, size, order);
        ArrayList<Ems4jEmployee> ems4jEmployees = new ArrayList<Ems4jEmployee>();
        for (CorpUser corpUser : departmentUser.getUserlist())
        {
            ems4jEmployees.add(convertCorpUser2Ems4jEmployee(corpUser));
        }
        return ems4jEmployees;
    }

    @Override
    public List<Ems4jEmployee> getDepartmentUserDetails(Long departmentId, Long offset, Integer size, Integer order)
    {
        CorpUserDetailList departmentUserDetails = dingTalk.getDepartmentUserDetails(departmentId, offset, size, order);
        ArrayList<Ems4jEmployee> ems4jEmployees = new ArrayList<Ems4jEmployee>();
        for (CorpUserDetail corpUserDetail : departmentUserDetails.getUserlist())
        {
            ems4jEmployees.add(convertCorpUser2Ems4jEmployee(corpUserDetail));
        }
        return ems4jEmployees;
    }

    private Ems4jEmployee convertCorpUser2Ems4jEmployee(CorpUser corpUser)
    {
        Ems4jEmployee ems4jEmployee = new Ems4jEmployee();
        Integer userid = null;
        try
        {
            userid = Integer.valueOf(corpUser.getUserid());
        } catch (NumberFormatException e)
        {
        }
        ems4jEmployee.setUserid(userid);
        ems4jEmployee.setName(corpUser.getName());
        if (corpUser instanceof CorpUserDetail)
        {
            CorpUserDetail corpUserDetail = (CorpUserDetail) corpUser;
            ems4jEmployee.setDepartment(corpUserDetail.getDepartment().get(0).intValue());
            ems4jEmployee.setMobile(Long.valueOf(corpUserDetail.getMobile()));
            ems4jEmployee.setEmail(corpUserDetail.getEmail());
            ems4jEmployee.setPosition(corpUserDetail.getPosition());
            Integer dingtel = null;
            try
            {
                dingtel = Integer.parseInt(corpUserDetail.getTel());
            } catch (NumberFormatException e)
            {
            }
            ems4jEmployee.setDingtel(dingtel);
        }
        return ems4jEmployee;
    }

    @Override
    public void sendTextMessage(String toUsers, String toParties, String agentId, String content)
    {
        Ems4jDingTalk ems4jDingTalk = new Ems4jDingTalk();
        ems4jDingTalk.setTouser(toUsers);
        ems4jDingTalk.setToparty(toParties);
        ems4jDingTalk.setCid(agentId);
        ems4jDingTalk.setSender("UNKNOWN");
        ems4jDingTalk.setContent(content);
        sendTextMessage(ems4jDingTalk);
    }

    @Override
    public void sendTextMessage(Ems4jDingTalk ems4jDingTalk)
    {
        dingTalk.sendTextMessage(ems4jDingTalk.getTouser(), ems4jDingTalk.getToparty(), ems4jDingTalk.getCid(), ems4jDingTalk.getContent());
        ems4jDingTalk.setSendTime(new Date());
        ems4jDingTalk.setMsgtype(MessageType.TEXT);
        ems4jDingTalk.setResultCode(MessageStatus.SUCCESS.getCode());
        ems4jDingTalkMapper.insert(ems4jDingTalk);
    }

    @Override
    public void sendImageMessage(String toUsers, String toParties, String agentId, String media_id)
    {
        Ems4jDingTalk ems4jDingTalk = new Ems4jDingTalk();
        ems4jDingTalk.setTouser(toUsers);
        ems4jDingTalk.setToparty(toParties);
        ems4jDingTalk.setCid(agentId);
        ems4jDingTalk.setSender("UNKNOWN");
        try
        {
            ems4jDingTalk.setMedia_id(Integer.parseInt(media_id));
        } catch (NumberFormatException e)
        {
        }
        sendImageMessage(ems4jDingTalk);
    }

    @Override
    public void sendImageMessage(Ems4jDingTalk ems4jDingTalk)
    {
        dingTalk.sendImageMessage(ems4jDingTalk.getTouser(), ems4jDingTalk.getToparty(), ems4jDingTalk.getCid(), ems4jDingTalk.getMedia_id().toString());
        ems4jDingTalk.setSendTime(new Date());
        ems4jDingTalk.setMsgtype(MessageType.IMAGE);
        ems4jDingTalk.setResultCode(MessageStatus.SUCCESS.getCode());
        ems4jDingTalkMapper.insert(ems4jDingTalk);
    }

    @Override
    public void getPage(Ems4jPage<Ems4jDingTalk> page, Map<String, Object> conditions)
    {
        //查询总数
        Long total = ems4jDingTalkMapper.selectCountByCondition(conditions);
        page.setTotalCount(total);

        //查询结果
        List<Ems4jDingTalk> list = ems4jDingTalkMapper.selectListByCondition(conditions, page);
        page.setResult(list);
    }
}
